﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace BF
{
    public interface ICollectRecycleItem : IRecycleable, IEnumerable
    {

    }

    public interface ICollectRecycleItem<TCollect> : ICollectRecycleItem, ICollection where TCollect : ICollection
    {
        TCollect Value { get; }
    }

    public interface ICollectRecycleItem<TCollect, TKey> : ICollectRecycleItem, ICollection<TKey>, IEnumerable<TKey> where TCollect : ICollection<TKey>
    {
        TCollect Value { get; }
    }


    public class CollectRecycleItem<TCollect, TKey> : ObjectRecycleItem, ICollectRecycleItem<TCollect, TKey> where TCollect : ICollection<TKey>
    {
        public TCollect Value { get; protected set; }

        public int Count => Value.Count;

        public bool IsReadOnly => Value.IsReadOnly;

        public CollectRecycleItem()
        {
            Value = Activator.CreateInstance<TCollect>();
        }

        public override void OnShow()
        {
            if (Value.Count > 0)
            {
                Debug.LogError($"[{this.GetType().Name}] [{typeof(TCollect).Name}<{typeof(TKey).Name}>] [OnShow Error] Collect is not empty");
            }
        }

        public override void OnRecycle()
        {
            Value.Clear();
            base.OnRecycle();
        }

        public override void Dispose()
        {
            Value.Clear();
            base.Dispose();
        }

        #region ICollect
        public IEnumerator<TKey> GetEnumerator()
        {
            return Value.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return Value.GetEnumerator();
        }

        public void Add(TKey item)
        {
            Value.Add(item);
        }

        public void Clear()
        {
            Value.Clear();
        }

        public bool Contains(TKey item)
        {
            return Value.Contains(item);
        }

        public void CopyTo(TKey[] array, int arrayIndex)
        {
            Value.CopyTo(array, arrayIndex);
        }

        public bool Remove(TKey item)
        {
            return Value.Remove(item);
        }
        #endregion ICollect
    }

    public class CollectRecycleItem2<TCollect, TKey> : CollectRecycleItem<TCollect, TKey>, ICollectRecycleItem<TCollect, TKey>, ICollectRecycleItem<TCollect> where TCollect : ICollection<TKey>, ICollection
    {
        public bool IsSynchronized => Value.IsSynchronized;

        public object SyncRoot => Value.SyncRoot;

        void ICollection.CopyTo(Array array, int index)
        {
            Value.CopyTo(array, index);
        }
    }
}